package dl.jf.web.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * @author jiajun
 * 
 */
public class Toolkit {

	private static String propertiesFileName = "custom.properties";
	private InputStream is = null;
	public static Logger logger = Logger.getLogger("Toolkit");

	/**
	 * @param paperdate
	 * @return boolean
	 */
	public static boolean isEmpty(String str) {
		if (str == null || str.length() == 0) {
			return true;
		}
		return false;
	}

	public static boolean isEmpty(List list) {
		if (list == null || list.isEmpty()) {
			return true;
		}
		return false;
	}
	
	public static boolean isEmpty(Object[] array) {
		if (array == null || array.length <= 0) {
			return true;
		}
		return false;
	}
	
	public String getCustomProperty(String key){
		return getCustomProperty(key, propertiesFileName);
	}

	public String getCustomProperty(String key, String fileName) {
		String content = "";
		try {
			Properties pro = new Properties();
			if (!isEmpty(fileName)) {
				is = getClass().getClassLoader().getResourceAsStream(fileName);
			} else {
				is = getClass().getClassLoader().getResourceAsStream(
						propertiesFileName);
			}
			pro.load(is);
			content = pro.getProperty(key);
		} catch (FileNotFoundException ex) {

		} catch (IOException ex) {

		} finally {
			try{
				is.close();
			} catch(Exception e){
				
			}
		}
		return content;
	}
	
	public static String getStringToUTF8(String str) {
		String result = "";
		try{
			if(!isEmpty(str)){
				result = new String(str.getBytes("iso-8859-1"),"UTF-8"); 
			}
		} catch(UnsupportedEncodingException ex) {
			logger.error(ex.getMessage(),ex);
		}
		return result;
	}
}
